package com.tbsoft.ehr.action.attendance;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionContext;
import com.tbsoft.ehr.action.BaseAction;
import com.tbsoft.ehr.model.AttendanceReport;
import com.tbsoft.ehr.service.attendance.ReportService;
/**
 * @desc 考勤报表action
 * @author vastdream
 *
 */
public class AttendanceReportAction extends BaseAction {
	private String dateStart;
	private String dateEnd;
	private String userName;
	private String scId;
	
	public synchronized String getScId() {
		return scId;
	}
	public synchronized void setScId(String scId) {
		this.scId = scId;
	}
	private List<AttendanceReport> infoList = new ArrayList<AttendanceReport>();
	
	private ReportService reportService;
	
	private Log log = LogFactory.getLog(getClass());
	public synchronized void setReportService(ReportService reportService) {
		this.reportService = reportService;
	}
	public synchronized List<AttendanceReport> getInfoList() {
		return infoList;
	}
	public synchronized void setInfoList(List<AttendanceReport> infoList) {
		this.infoList = infoList;
	}
	
	public synchronized String getDateStart() {
		return dateStart;
	}
	public synchronized void setDateStart(String dateStart) {
		this.dateStart = dateStart;
	}
	public synchronized String getDateEnd() {
		return dateEnd;
	}
	public synchronized void setDateEnd(String dateEnd) {
		this.dateEnd = dateEnd;
	}
	public synchronized String getUserName() {
		return userName;
	}
	public synchronized void setUserName(String userName) {
		this.userName = userName;
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private void initDate(){
		String month = new SimpleDateFormat("yyyy-MM").format(new Date());
		if(dateStart == null || dateStart.isEmpty()){
			dateStart = month +"-1";
		}
		if(dateEnd == null || dateEnd.isEmpty()){
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Calendar cal = Calendar.getInstance();
			try {
				cal.setTime(df.parse(dateStart));
			} catch (ParseException e) {
				dateEnd = dateStart;
			}
			cal.add(Calendar.MONTH, 1);
			cal.add(Calendar.DATE, -1);
			dateEnd = df.format(cal.getTime());
		}
	}
	
	public String info(){
		initDate();
		return "infolist";
	}
	public String report(){
		initDate();
		return "reportlist";
	}
	public String listinfo(){
		infoList.clear();
		try{
			List<AttendanceReport> tmpList = reportService.getAttendacneInfo(dateStart,dateEnd, userName);
			infoList.addAll(tmpList);
		}catch(Exception e){
			log.error("listinfo exception", e);
			errorReturn("考勤明细查询失败");
		}
		return "infolist";
	}
	public String listreport(){
		infoList.clear();
		try{
			Map map =(Map) ActionContext.getContext().getApplication().get("PUBFIELD_MAP");
			reportService.setPubFieldMap(map);
			List<AttendanceReport> tmpList = reportService.getAttendacneReport(dateStart,dateEnd,scId);
			infoList.addAll(tmpList);
		}catch(Exception e){
			log.error("listinfo exception", e);
			errorReturn("考勤报表查询失败");
		}
		return "reportlist";
	}

}
